package com.example.magictowerx;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.microedition.lcdui.game.LayerManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class GameScreen extends GameView
{
    private Paint			paint		= null;
    public MainGame			mMainGame	= null;

    public static final int TEXT_COLOR = 0xffc800;
    public static final int BACK_COLOR = 0x000000;
    public static final int UP = 0,DOWN = 1,LEFT = 2,RIGHT = 3;
    public static final int MILLIS_PER_TICK = 300;

    private LayerManager layerManager;
    private boolean mshowMessage = false;
    public boolean mshowDialog = false;
    public boolean mshowFight = false;
    private String strMessage = "";
    public FightScreen mFightScreen;

    private HeroSprite hero;
    private GameMap gameMap;
    private FightCalc fightCalc;
    private Task task;
    private static final int step = GameMap.TILE_WIDTH;
    public Canvas mcanvas;
    public static final int IMAGE_HERO = 0,
            IMAGE_MAP = 1,
            IMAGE_DIALOG_HERO = 2,
            IMAGE_DIALOG_ANGLE = 3,
            IMAGE_DIALOG_THIEF = 4,
            IMAGE_BORDER = 5,
            IMAGE_DIALOGBOX = 6,
            IMAGE_MESSAGEBOX = 7,
            IMAGE_BORDER2 = 8,
            IMAGE_BORDER3 = 9,
            IMAGE_BORDER4 = 10,
            IMAGE_DIALOG_PRINCESS = 11,
            IMAGE_DIALOG_BOSS = 12,
            IMAGE_BLUE_GEEZER = 13,
            IMAGE_RED_GEEZER = 14,
            IMAGE_SPLASH = 15,
            IMAGE_GAMEOVER = 16,
            IMAGE_WHEEL = 17,
            IMAGE_BTNA = 18,
            IMAGE_BTNB = 19;

    public int					borderX, borderY;
    private int				winWidth, winHeight;
    private int				scrollX, scrollY;
    private int				curDialogImg;
    private MagicTower			magicTower;

    public TextUtil				tu				= null;
    private int				miType			= -1;

    public GameScreen(Context context, MagicTower magicTower, MainGame mainGame, boolean isNewGame)
    {
        super(context);
        mMainGame = mainGame;
        paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        this.magicTower = magicTower;
        winWidth = MySet.BORDERW; //沙盒宽
        winHeight = MySet.BORDERH; //沙盒高
        borderX = MySet.BORDERX; //沙盒横坐标
        borderY = 80; //沙盒纵坐标
        layerManager = new LayerManager();
        tu = new TextUtil();
        hero = new HeroSprite(BitmapFactory.decodeResource(this.getResources(), R.mipmap.hero17), GameMap.TILE_WIDTH, GameMap.TILE_HEIGHT);
        hero.defineReferencePixel(GameMap.TILE_WIDTH / 2, GameMap.TILE_HEIGHT / 2);
        gameMap = new GameMap(hero, BitmapFactory.decodeResource(this.getResources(), R.mipmap.map17));
        fightCalc = new FightCalc(hero);
        task = new Task(this, hero, gameMap);
        hero.setTask(task);
        if (isNewGame == false)
        {
            load();
        }
        layerManager.append(hero);
        layerManager.append(gameMap.getFloorMap());
    }


    protected void onDraw(Canvas canvas)
    {
        mcanvas = canvas;
        paint.setColor(Color.BLACK);
        MySet.fillRect(canvas, 0, 0, MySet.SCREENW, MySet.SCREENH, paint);

        drawAttr(canvas);
        drawCont(canvas);
        gameMap.animateMap();
        scrollWin();
        layerManager.setViewWindow(scrollX, scrollY, winWidth, winHeight);
        canvas.save();
        layerManager.paint(canvas, borderX, borderY);

        if (mshowMessage)
        {
            showMessage(strMessage);
        }
        if (mshowDialog)
        {
            dialog();
        }
        if (mFightScreen != null && mshowFight)
        {
            mFightScreen.onDraw(canvas);
        }
    }

    public boolean onTouchEvent(MotionEvent event)
    {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int type = 0;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mFightScreen != null && mshowFight)
                {
                    mFightScreen.onTouchEvent(event);
                    return false;
                }
                // 记录触摸点坐标
                if (MySet.touchUp(x ,y ))
                {
                    hero.setFrame(9); //设置显示帧
                    if ((type = gameMap.canPass(UP)) == 1)
                    {
                        hero.move(0, -step); //移动
                    }
                }
                else if (MySet.touchDown(x,y))
                {
                    hero.setFrame(0);
                    if ((type = gameMap.canPass(DOWN)) == 1)
                    {
                        hero.move(0, step);
                    }
                }
                else if (MySet.touchRight(x,y))
                {
                    hero.setFrame(6);
                    if ((type = gameMap.canPass(RIGHT)) == 1)
                    {
                        hero.move(step, 0);
                    }
                }
                else if (MySet.touchLeft(x,y))
                {
                    hero.setFrame(3);
                    if ((type = gameMap.canPass(LEFT)) == 1)
                    {
                        hero.move(-step, 0);
                    }
                }
                else if (MySet.touchOK(x,y))
                {
                    if (mshowMessage)
                    {
                        // 直到无法翻页为止
                        if (!tu.Key(MySet.KEY_DPAD_DOWN))
                        {
                            mshowMessage = false;
                            if ((miType >= GameMap.MAP_YELLOW_KEY) && (miType <= GameMap.MAP_SHIELD3))
                            {
                                miType = -1;
                                gameMap.remove();
                            }
                        }
                    }
                    else if (mshowDialog)
                    {
                        if (!tu.Key(MySet.KEY_DPAD_DOWN))
                        {
                            if (task.mbtask)
                            {
                                if (task.curTask2 < Task.finishedDialog[task.curTask].length - 1)
                                {
                                    task.curTask2++;
                                    tu.InitText(Task.finishedDialog[task.curTask][task.curTask2], 0, (MySet.SCREENH - MySet.MessageBoxH) / 2, MySet.SCREENW,
                                            MySet.MessageBoxH, 0x0, 0xffff00, 255, MySet.TextSize);
                                }
                                else
                                {
                                    task.curTask2 = 0;
                                    mshowDialog = false;
                                }
                            }
                            else
                            {
                                if (task.curTask2 < Task.recieveDialog[task.curTask].length - 1)
                                {
                                    task.curTask2++;
                                    tu.InitText(Task.recieveDialog[task.curTask][task.curTask2], 0, (MySet.SCREENH - MySet.MessageBoxH) / 2, MySet.SCREENW,
                                            MySet.MessageBoxH, 0x0, 0xffff00, 255, MySet.TextSize);
                                }
                                else
                                {
                                    task.curTask2 = 0;
                                    mshowDialog = false;
                                }
                            }
                        }
                    }
                }
                else if (MySet.touchBack(x,y))
                {
                    save();
                    mMainGame.controlView(MySet.GAME_MENU);
                    if (mMainGame.mbMusic == 1)
                    {
                        mMainGame.mCMIDIPlayer.PlayMusic(1);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        if (type >= GameMap.MAP_LOCKED_BARRIER)
            dealType(type);
        return true;
    }

    public boolean onKeyUp(int keyCode)
    {
        int type = 0;
        if (keyCode == KeyEvent.KEYCODE_1)
        {
            mMainGame.mCMIDIPlayer.StopMusic();
            return false;
        }
        if (keyCode == KeyEvent.KEYCODE_2)
        {
            mMainGame.mCMIDIPlayer.PlayMusic(2);
        }
        if (mFightScreen != null && mshowFight)
        {
            mFightScreen.onKeyUp(keyCode);
            return false;
        }
        if ((mshowMessage && keyCode != MySet.KEY_DPAD_OK) || (mshowDialog && keyCode != MySet.KEY_DPAD_OK))
        {
            return false;
        }
        switch (keyCode)
        {
//            case MySet.KEY_DPAD_OK:
//                save();
//                mMainGame.controlView(MySet.GAME_MENU);
//                if (mMainGame.mbMusic == 1)
//                {
//                    mMainGame.mCMIDIPlayer.PlayMusic(1);
//                }
//                break;
            case MySet.KEY_DPAD_OK:
                if (mshowMessage)
                {
                    // 直到无法翻页为止
                    if (!tu.Key(MySet.KEY_DPAD_DOWN))
                    {
                        mshowMessage = false;
                        if ((miType >= GameMap.MAP_YELLOW_KEY) && (miType <= GameMap.MAP_SHIELD3))
                        {
                            miType = -1;
                            gameMap.remove();
                        }
                    }
                }
                else if (mshowDialog)
                {
                    if (!tu.Key(MySet.KEY_DPAD_DOWN))
                    {
                        if (task.mbtask)
                        {
                            if (task.curTask2 < Task.finishedDialog[task.curTask].length - 1)
                            {
                                task.curTask2++;
                                tu.InitText(Task.finishedDialog[task.curTask][task.curTask2], 0, (MySet.SCREENH - MySet.MessageBoxH) / 2, MySet.SCREENW,
                                        MySet.MessageBoxH, 0x0, 0xffff00, 255, MySet.TextSize);
                            }
                            else
                            {
                                task.curTask2 = 0;
                                mshowDialog = false;
                            }
                        }
                        else
                        {
                            if (task.curTask2 < Task.recieveDialog[task.curTask].length - 1)
                            {
                                task.curTask2++;
                                tu.InitText(Task.recieveDialog[task.curTask][task.curTask2], 0, (MySet.SCREENH - MySet.MessageBoxH) / 2, MySet.SCREENW,
                                        MySet.MessageBoxH, 0x0, 0xffff00, 255, MySet.TextSize);
                            }
                            else
                            {
                                task.curTask2 = 0;
                                mshowDialog = false;
                            }
                        }
                    }
                }
                break;
            case MySet.KEY_DPAD_UP:
                hero.setFrame(9); //设置显示帧
                if ((type = gameMap.canPass(UP)) == 1)
                {
                    hero.move(0, -step); //移动
                }
                break;
            case MySet.KEY_DPAD_DOWN:
                hero.setFrame(0);
                if ((type = gameMap.canPass(DOWN)) == 1)
                {
                    hero.move(0, step);
                }
                break;
            case MySet.KEY_DPAD_LEFT:
                hero.setFrame(3);
                if ((type = gameMap.canPass(LEFT)) == 1)
                {
                    hero.move(-step, 0);
                }
                break;
            case MySet.KEY_DPAD_RIGHT:
                hero.setFrame(6);
                if ((type = gameMap.canPass(RIGHT)) == 1)
                {
                    hero.move(step, 0);
                }
                break;
            default:
                break;
        }
        if (type >= GameMap.MAP_LOCKED_BARRIER)
            dealType(type);
        return true;
    }


    public boolean onKeyDown(int keyCode)
    {
        return true;
    }


    public void refurbish()
    {

    }


    public void reCycle()
    {
        paint = null;
        System.gc();
    }

    //判断相遇的情况
    private void dealType(int type)
    {
        if (type == GameMap.MAP_UPSTAIR) //上楼
        {
            gameMap.upstair();
            hero.setFrame(0);
        }
        else if (type == GameMap.MAP_DOWNSTAIR)  //下楼
        {
            gameMap.downstair();
            hero.setFrame(0);
        }
        else if ((type >= GameMap.MAP_YELLOW_DOOR) && (type <= GameMap.MAP_RED_DOOR))  //钥匙
        {
            if (hero.useKey(type))
            {
                gameMap.remove();
            }
        }
        else if (type == GameMap.MAP_BARRIER) //
        {
            gameMap.remove();
        }
        else if ((type == GameMap.MAP_SHOP1) || (type == GameMap.MAP_SHOP2)) //商店
        {
            if (gameMap.curFloorNum == 3)
            {
                // shop(ShopScreen.SHOP_3);
            }
            else
            {
                // shop(ShopScreen.SHOP_11);
            }
        }
        else if ((type >= GameMap.MAP_YELLOW_KEY) && (type <= GameMap.MAP_SHIELD3)) //物品
        {
            mshowMessage = true;
            miType = type;
            tu.InitText(hero.takeGem(type), 0, (MySet.SCREENH - MySet.MessageBoxH) / 2, MySet.SCREENW, MySet.MessageBoxH, 0x0, 0xff0000, 255, MySet.TextSize);
        }
        else if (type >= GameMap.MAP_ANGLE) // 人物
        {
            if (type > GameMap.MAP_ORGE31)
                type -= GameMap.SWITCH_OFFSET;
            if (((type >= GameMap.MAP_ANGLE) && (type <= GameMap.MAP_RED_GEEZER)) || (type == GameMap.MAP_ORGE31)) //npc
            {
                /* 处理任务，注意不同层的老头的任务不同 */
                dealTask(type);
            }
            else if ((type >= GameMap.MAP_ORGE1) && (type <= GameMap.MAP_ORGE30)) //怪物
            {
                fight(type);
            }
        }
    }


    public void showMessage(String msg)
    {
        int w = MySet.SCREENW;
        int h = MySet.MessageBoxH;
        int x = 0;
        int y = (MySet.SCREENH - MySet.MessageBoxH) / 2;
        Paint ptmPaint = new Paint();
        ptmPaint.setARGB(255, Color.red(BACK_COLOR), Color.green(BACK_COLOR), Color.blue(BACK_COLOR));

        MySet.fillRect(mcanvas, x, y, w, h, ptmPaint);

        tu.DrawText(mcanvas);
        ptmPaint = null;
    }

    private void drawCont(Canvas canvas) //操作栏
    {
        MySet.drawImage(canvas, getImage(IMAGE_WHEEL), 160, 1350, 400, 400, 0, 0);
        MySet.drawImage(canvas, getImage(IMAGE_BTNA), 600, 1380, 240, 240, 0, 0);
        MySet.drawImage(canvas, getImage(IMAGE_BTNB), 800, 1520, 240, 240, 0, 0);
    }

    private void drawAttr(Canvas canvas) //状态栏
    {
        int iH = MySet.TextSize;
        Paint ptmPaint = new Paint();
        ptmPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        ptmPaint.setARGB(255, Color.red(0xffff00), Color.green(0xffff00), Color.blue(0xffff00));

        MySet.drawRect(canvas, 28, 1152, 1024, 192, ptmPaint);
        canvas.drawLine(252, 1152, 252, 1344, ptmPaint);
        canvas.drawLine(764, 1152, 764, 1344, ptmPaint);

        MySet.drawRect(canvas, 0, 0, 1080, 80, ptmPaint);

        MySet.drawImage(canvas, getImage(IMAGE_HERO), 92, 1180, 96, 96, 0, 0);

        ptmPaint.setTextSize(MySet.TextSize);
        ptmPaint.setARGB(255, Color.red(0xffffff), Color.green(0xffffff), Color.blue(0xffffff));
        // 等级
        MySet.drawString(canvas, "等级：" + hero.getLevel(), 252, 1172, ptmPaint);
        MySet.drawString(canvas, "经验：" + hero.getExperience(), 252, 1172 + 1 * iH, ptmPaint);
        MySet.drawString(canvas, "金币：" + hero.getMoney(), 252, 1172 + 2 * +iH, ptmPaint);

        MySet.drawString(canvas, "生命：" + hero.getHp(), 500, 1172, ptmPaint);
        MySet.drawString(canvas, "攻击：" + hero.getAttack(), 500, 1172 + 1 * iH, ptmPaint);
        MySet.drawString(canvas, "防御：" + hero.getDefend(), 500, 1172 + 2 * +iH, ptmPaint);

        MySet.drawString(canvas, "红钥匙：" + hero.getRedKey(), 764, 1172, ptmPaint);
        MySet.drawString(canvas, "黄钥匙：" + hero.getYellowKey(), 764, 1172 + 1 * iH, ptmPaint);
        MySet.drawString(canvas, "蓝钥匙：" + hero.getBlueKey(), 764, 1172 + 2 * +iH, ptmPaint);

        String string = "《序章》";
        if (gameMap.curFloorNum != 0)
        {
            string = "《第" + gameMap.curFloorNum + "层》";
        }
        MySet.drawString(canvas, string, (224 - ptmPaint.measureText(string)) / 2 + 28, 1280, ptmPaint);

        ptmPaint.setTextSize(36);
        string = "《魔塔Android版》谢谢使用！";
        MySet.drawString(canvas, string, (MySet.BORDERW - ptmPaint.measureText(string)) / 2, (80 - 36) / 2, ptmPaint);

        ptmPaint = null;
    }


    private void scrollWin()
    {
        scrollX = 0;
        scrollY = 0;
//        scrollX = hero.getRefPixelX() - winWidth / 2;
//        scrollY = hero.getRefPixelY() - winHeight / 2;
//        if (scrollX < 0)
//        {
//            scrollX = 0;
//        }
//        else if ((scrollX + winWidth) > GameMap.MAP_WIDTH)
//        {
//            scrollX = GameMap.MAP_WIDTH - winWidth;
//        }
//        if (scrollY < 0)
//        {
//            scrollY = 0;
//        }
//        else if ((scrollY + winHeight) > GameMap.MAP_HEIGHT)
//        {
//            scrollY = GameMap.MAP_HEIGHT - winHeight;
//        }
    }


    public boolean fight(int type)
    {
        if (fightCalc.canAttack(type) == false) //打不过
            return false;
        mFightScreen = new FightScreen(this, fightCalc, hero, type);
        mshowFight = true;
        gameMap.remove();
        return true;
    }


    private void shop(int type)
    {
        // 商店
    }


    private void jump()
    {
        // 跳
    }


    private void lookup()
    {
        // 查看
    }


    private void dealTask(int type)  //npc事件
    {
        int curTask = -1;
        switch (type)
        {
            case GameMap.MAP_ANGLE:
                curTask = Task.FIND_CROSS;
                curDialogImg = IMAGE_DIALOG_ANGLE;
                break;
            case GameMap.MAP_THIEF:
                curTask = Task.FIND_AX;
                curDialogImg = IMAGE_DIALOG_THIEF;
                break;
            case GameMap.MAP_PRINCESS:
                curTask = Task.RESCUE_PRINCESS;
                curDialogImg = IMAGE_DIALOG_PRINCESS;
                break;
            case GameMap.MAP_BLUE_GEEZER:
                switch (gameMap.curFloorNum)
                {
                    case 2:
                        curTask = Task.GET_QINGFEND_JIAN;
                        curDialogImg = IMAGE_BLUE_GEEZER;
                        mshowMessage = true;
                        miType = type;
                        tu.InitText(hero.takeGem(GameMap.MAP_SWORD3), 0, (MySet.SCREENH - MySet.MessageBoxH) / 2, MySet.SCREENW, MySet.MessageBoxH, 0x0,
                                0xff0000, 255, MySet.TextSize);
                        // showMessage(mcanvas,hero.takeGem(GameMap.MAP_SWORD3));
                        break;
                    case 5:
                        // shop(ShopScreen.SHOP_5_1);
                        break;
                    case 13:
                        // shop(ShopScreen.SHOP_13);
                        break;
                    case 15:
                        curTask = Task.GET_SHENGGUANG_JIAN;
                        curDialogImg = IMAGE_BLUE_GEEZER;
                        break;
                }
                break;
            case GameMap.MAP_RED_GEEZER:
                switch (gameMap.curFloorNum)
                {
                    case 2:
                        curTask = Task.GET_HUANGJIN_DUN;
                        curDialogImg = IMAGE_RED_GEEZER;
                        mshowMessage = true;
                        miType = type;
                        tu.InitText(hero.takeGem(GameMap.MAP_SHIELD3), 0, (MySet.SCREENH - MySet.MessageBoxH) / 2, MySet.SCREENW, MySet.MessageBoxH, 0x0,
                                0xff0000, 255, MySet.TextSize);
                        // showMessage(mcanvas,hero.takeGem(GameMap.MAP_SHIELD3));
                        break;
                    case 5:
                        // shop(ShopScreen.SHOP_5_2);
                        break;
                    case 12:
                        // shop(ShopScreen.SHOP_12);
                        break;
                    case 15:
                        curTask = Task.GET_XINGGUANG_DUN;
                        curDialogImg = IMAGE_RED_GEEZER;
                        break;
                }
                break;
            case GameMap.MAP_ORGE31:
                curTask = Task.FIGHT_BOSS;
                curDialogImg = IMAGE_DIALOG_BOSS;
                break;
        }
        if (curTask == -1)
            return;
        task.execTask(curTask);
    }


    public void dialog()  //对话
    {
        int x, y, w, h;
        w = MySet.SCREENW;
        h = MySet.MessageBoxH;
        x = 0;
        y = (MySet.SCREENH - MySet.MessageBoxH) / 2;

        if (task.curTask2 % 2 == 0)
        {
            drawDialogBox(IMAGE_DIALOG_HERO, x, y, w, h);
        }
        else
        {
            drawDialogBox(curDialogImg, x, y, w, h);
        }

        tu.DrawText(mcanvas);
    }


    private void drawDialogBox(int imgType, int x, int y, int w, int h)  //绘制对话框
    {
        Paint ptmPaint = new Paint();
        ptmPaint.setARGB(255, Color.red(BACK_COLOR), Color.green(BACK_COLOR), Color.blue(BACK_COLOR));

        MySet.fillRect(mcanvas, x, y, w, h, ptmPaint);

        Bitmap img = getImage(imgType);

        MySet.drawRect(mcanvas, x, y, w, h, ptmPaint);
        if (img != null)
        {
            if (imgType == IMAGE_DIALOG_HERO)
            {
                MySet.drawImage(mcanvas, img, x, y - 64);
            }
            else
            {
                MySet.drawImage(mcanvas, img, MySet.SCREENW - 40, y - 64);
            }
        }
        ptmPaint = null;
    }


    protected void keyPressed(int keyCode)
    {

        // switch(keyCode){
        // case GameCanvas.KEY_NUM1: jump();break;
        // case GameCanvas.KEY_NUM3: lookup();break;
        // }
    }


    public void end()
    {
        // stop();
        // EndScreen end = new EndScreen(display,menu);
        // display.setCurrent(end);
        // end.start();
    }


    public Bitmap getImage(int type)
    {
        Bitmap result = null;
        switch (type)
        {
            case IMAGE_HERO:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.hero17);
                break;
            case IMAGE_MAP:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.map17);
                break;
            case IMAGE_DIALOG_HERO:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.dialog_hero);
                break;
            case IMAGE_DIALOG_ANGLE:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.dialog_angle);
                break;
            case IMAGE_DIALOG_THIEF:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.dialog_thief);
                break;
            case IMAGE_DIALOG_PRINCESS:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.dialog_princess);
                break;
            case IMAGE_DIALOG_BOSS:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.dialog_boss);
                break;
            case IMAGE_BLUE_GEEZER:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.dialog_bluegeezer);
                break;
            case IMAGE_RED_GEEZER:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.dialog_redgeezer);
                break;
            case IMAGE_GAMEOVER:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.gameover);
                break;
            case  IMAGE_WHEEL:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.wheel_1);
                break;
            case  IMAGE_BTNA:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.btna_1);
                break;
            case  IMAGE_BTNB:
                result = BitmapFactory.decodeResource(this.getResources(), R.mipmap.btnb_1);
                break;
        }
        return result;
    }


    boolean save()
    {
        int col = hero.getRefPixelX() / GameMap.TILE_WIDTH;
        int row = hero.getRefPixelY() / GameMap.TILE_HEIGHT;
        byte[] r1 = hero.encode();
        byte[] r2 = { (byte) gameMap.curFloorNum, (byte) gameMap.reachedHighest, (byte) row, (byte) col, (byte) hero.getFrame() };
        byte[] r3 = task.getTask();

        Properties properties = new Properties();

        properties.put("music", String.valueOf(mMainGame.mbMusic));

        properties.put("r1l", String.valueOf(r1.length));
        properties.put("r2l", String.valueOf(r2.length));
        properties.put("r3l", String.valueOf(r3.length));
        for (int i = 0; i < r1.length; i++)
        {
            properties.put("r1_" + i, String.valueOf(r1[i]));
        }
        for (int i = 0; i < r2.length; i++)
        {
            properties.put("r2_" + i, String.valueOf(r2[i]));
        }
        for (int i = 0; i < r3.length; i++)
        {
            properties.put("r3_" + i, String.valueOf(r3[i]));
        }

        for (int i = 0; i < GameMap.FLOOR_NUM; i++)
        {
            byte map[] = gameMap.getFloorArray(i);
            for (int j = 0; j < map.length; j++)
            {
                properties.put("map_" + i + "_" + j, String.valueOf(map[j]));
            }
        }

        try
        {
            FileOutputStream stream = magicTower.openFileOutput("save", Context.MODE_WORLD_WRITEABLE);
            properties.store(stream, "");
        }
        catch (FileNotFoundException e)
        {
            return false;
        }
        catch (IOException e)
        {
            return false;
        }

        return true;
    }


    boolean load()
    {
        Properties properties = new Properties();
        try
        {
            FileInputStream stream = magicTower.openFileInput("save");

            properties.load(stream);
        }
        catch (FileNotFoundException e)
        {
            return false;
        }
        catch (IOException e)
        {
            return false;
        }

        mMainGame.mbMusic = Byte.valueOf(properties.get("music").toString());

        byte[] r1 = new byte[Byte.valueOf(properties.get("r1l").toString())];
        byte[] r2 = new byte[Byte.valueOf(properties.get("r2l").toString())];
        byte[] r3 = new byte[Byte.valueOf(properties.get("r3l").toString())];

        for (int i = 0; i < r1.length; i++)
        {
            r1[i] = Byte.valueOf(properties.get("r1_" + i).toString());
        }
        for (int i = 0; i < r2.length; i++)
        {
            r2[i] = Byte.valueOf(properties.get("r2_" + i).toString());
        }
        for (int i = 0; i < r3.length; i++)
        {
            r3[i] = Byte.valueOf(properties.get("r3_" + i).toString());
        }

        hero.decode(r1);
        gameMap.curFloorNum = r2[0];
        gameMap.reachedHighest = r2[1];
        hero.setFrame(r2[4]);
        task.setTask(r3);

        for (int i = 0; i < GameMap.FLOOR_NUM; i++)
        {
            byte[] map = new byte[GameMap.TILE_NUM];
            for (int j = 0; j < map.length; j++)
            {
                map[j] = Byte.valueOf(properties.get("map_" + i + "_" + j).toString());
            }

            gameMap.setFloorArray(i, map);
        }

        gameMap.setMap(gameMap.curFloorNum);

        hero.setRefPixelPosition(r2[3] * GameMap.TILE_WIDTH + GameMap.TILE_WIDTH / 2, r2[2] * GameMap.TILE_HEIGHT + GameMap.TILE_HEIGHT / 2);

        return true;
    }
}


